package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.*;
import cevent.source.cloudcenter.server.service.StoreService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author cevent
 * @description 店铺管理表控制层
 */
@RestController
@RequestMapping("/admin/store")
public class StoreController {
    private static final Logger LOG = LoggerFactory.getLogger(StoreController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME = "模块管理 店铺管理表";
    @Resource
    private StoreService storeService;

    //1.dto-store获取
    @RequestMapping("/dtoList")
    public List<StoreDto> getDtoList() {
        return storeService.storeDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageSelectDto pageSelectDto) {
        LOG.info("查看controller的pageDto：{}", pageSelectDto);
        ResponseDataDto responseData = new ResponseDataDto();
        if(pageSelectDto.getProvinceName()!=null){
            storeService.getProvincePage(pageSelectDto);
        }else if(pageSelectDto.getCityName()!=null){
            storeService.getCityPage(pageSelectDto);
        }else if(pageSelectDto.getPayStatus()!=null){
            storeService.getPayStatusPage(pageSelectDto);
        }else if(pageSelectDto.getStoreFrontType()!=null){
            storeService.getStoreFrontTypePage(pageSelectDto);
        }else if(pageSelectDto.getStoreType()!=null){
            //这里注意storeType必须设置为str，在执行时转Integer.parseInt(str)，否则默认生成type=0
            storeService.getStoreTypePage(pageSelectDto);
        }else{
            storeService.getStorePageDtoList(pageSelectDto);
        }
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody StoreDto storeDto) {
        LOG.info("传入的store DTO：{}", storeDto);

        ValidatorUtil.requiredLength(storeDto.getDefName(), "店铺名称", 3, 255);
        /*ValidatorUtil.requiredLength(storeDto.getFullName(), "门店全称", 3, 255);*/
        ValidatorUtil.requiredLength(storeDto.getManagerTel(), "手机号", 3, 255);
        ValidatorUtil.requiredEmpty(storeDto.getPayFull(), "全款金额");
        /*ValidatorUtil.requiredEmpty(storeDto.getPayWechatAmount(), "微信支付");
        ValidatorUtil.requiredEmpty(storeDto.getPayAliAmount(), "支付宝支付");
        ValidatorUtil.requiredEmpty(storeDto.getPayBalanceAmount(), "余额支付");
        ValidatorUtil.requiredEmpty(storeDto.getPayBankAmount(), "银行卡支付");
        ValidatorUtil.requiredEmpty(storeDto.getPayCashAmount(), "现金支付");
        ValidatorUtil.requiredLength(storeDto.getStoreRemark(), "店铺说明", 3, 255);*/
        ValidatorUtil.requiredEmpty(storeDto.getSubsidy(), "店铺门头补贴");
        /*ValidatorUtil.requiredLength(storeDto.getSubsidyRemark(), "补贴说明", 3, 255);*/
        ResponseDataDto responseData = new ResponseDataDto();
        storeService.save(storeDto);
        responseData.setResponseData(storeDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的store uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        storeService.delete(uniId);
        return responseData;
    }

    //支付凭证上传
    @PostMapping("/savePicList")
    public ResponseDataDto savePicList(@RequestBody ModulePicPathDto modulePicPathDto) {
        ResponseDataDto responseData = new ResponseDataDto();
        responseData.setResponseData(storeService.savePicList(modulePicPathDto));
        return responseData;
    }

    //门头上传
    @PostMapping("/saveStoreFront")
    public ResponseDataDto saveStoreFront(@RequestBody ModulePicPathDto modulePicPathDto) {
        ResponseDataDto responseData = new ResponseDataDto();
        responseData.setResponseData(storeService.saveStoreFront(modulePicPathDto));
        return responseData;
    }

    //条件查询（分页）************
    //1.根据省份查询
    @PostMapping("/getProvincePage")
    public ResponseDataDto provinceGet(@RequestBody PageSelectDto pageSelectDto) {
        LOG.info("根据省份查询：{}", pageSelectDto);
        ResponseDataDto responseData = new ResponseDataDto();
        storeService.getProvincePage(pageSelectDto);
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }
    //2.根据城市模糊查询
    @PostMapping("/getCityPage")
    public ResponseDataDto getCityPage(@RequestBody PageSelectDto pageSelectDto){
        LOG.info("根据城市模糊查询：{}", pageSelectDto);
        ResponseDataDto responseData=new ResponseDataDto();
        storeService.getCityPage(pageSelectDto);
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }
    //3.根据支付状态查询
    @PostMapping("/getPayStatusPage")
    public ResponseDataDto getPayStatusPage(@RequestBody PageSelectDto pageSelectDto){
        LOG.info("根据支付状态查询：{}", pageSelectDto);
        ResponseDataDto responseData=new ResponseDataDto();
        storeService.getPayStatusPage(pageSelectDto);
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }
    //4.根据门头达标状态查询
    @PostMapping("/getStoreFrontTypePage")
    public ResponseDataDto getStoreFrontTypePage(@RequestBody PageSelectDto pageSelectDto){
        LOG.info("根据门头达标状态查询：{}", pageSelectDto);
        ResponseDataDto responseData=new ResponseDataDto();
        storeService.getStoreFrontTypePage(pageSelectDto);
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }

    //5.根据店铺类型查询
    @PostMapping("/getStoreTypePage")
    public ResponseDataDto getStoreTypePage(@RequestBody PageSelectDto pageSelectDto){
        LOG.info("根据门头达标状态查询：{}", pageSelectDto);
        ResponseDataDto responseData=new ResponseDataDto();
        storeService.getStoreTypePage(pageSelectDto);
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }

    /**
     * 6.导出excel(xlsx)便于用户下载选择目录及文件名
     * 注意这里没有response，如果添加会报异常：ResponseDataDto] with preset Content-Type 'application/vnd.ms-excel;charset=utf-8'
     * @return
     */
    @RequestMapping("/getStoreExcel")
    public void getStoreExcel(HttpServletResponse httpServletResponse){
        /*httpServletResponse.setContentType("application/vnd.ms-excel");*/
        ResponseDataDto responseData=new ResponseDataDto();
        storeService.exportStoreExcel(httpServletResponse);
        responseData.setResponseData("导出店铺数据成");
    }
    //7.导入excel生成数据
    @PostMapping("/importStoreExcel")
    public ResponseDataDto importStoreExcel(MultipartFile file){
        ResponseDataDto responseData=new ResponseDataDto();
        storeService.importStoreExcel(file);
        responseData.setResponseMsg("导入store数据成功");
        return responseData;
    }
}
